<template>

    <div class="setconf">
        <el-row class="py-1">
            <el-col :span="6">
              <el-tooltip   class="box-item"  effect="dark"  content="快排时，最大页面查询深度数,根据IP时效性，建议为6"      placement="top-start" >
                  <el-text tag="sub" size="small" class="underline white">查询页数深度</el-text>
              </el-tooltip>
            </el-col>
            <el-col :span="18"><el-input-number tag="sub" :min="1" :max="10" size="small" v-model="depth_max" class="w-20 white" placeholder="最大页面" /></el-col>
        </el-row>

        <el-row class="py-1">
            <el-col :span="6">
              <el-tooltip   class="box-item"  effect="dark"  content="搜索到页面时,进行转化有效率性的停留。取2个值之前的随机时间" placement="top-start" >
                <el-text tag="sub" size="small" class="underline white" >搜索停留</el-text>
              </el-tooltip>
            </el-col>
            <el-col :span="18" class="flex  ">
              <el-input-number  size="small"  class="w-20" v-model="search_stay_min_num" :min="1" :max="5"   />
              &nbsp;&nbsp;&nbsp;&nbsp;
              <el-input-number  size="small"  class="w-20" v-model="search_stay_max_num" :min="1" :max="10"  />
            </el-col>
        </el-row>

        <el-row class="py-1">
            <el-col :span="6">
              <el-tooltip   class="box-item"  effect="dark"  content="最大线程数:词超多可以增加线程数,线程数越大，占用服务器CPU、内存更多。系统使用的动态线程池，即根据系统负载情况动态调整线程池大小。"  placement="top-start" >
                <el-text tag="sub" size="small" class="underline white">最大线程数</el-text>
              </el-tooltip>
            </el-col>
            <el-col :span="18"><el-input-number tag="sub" :min="1" :max="10" class="w-20" size="small" v-model="threads_max_num"  /></el-col>
        </el-row>

        <el-row class="py-1">
            <el-col :span="6">
              <el-tooltip   class="box-item"  effect="dark"  content="最低内存数:单位为M。1、最小为0，表示不启动此功能;最大数为2M(2048字节)。2、达到内存最低数，系统无限的自动等待内存下降。可防止(远程)服务器无法登录或进行其它操作"  placement="top-start" >
                <el-text tag="sub" size="small" class="underline white">最低内存数</el-text>
              </el-tooltip>
            </el-col>
            <el-col :span="18"><el-input-number tag="sub" :min="0" :max="2048" class="w-50" size="small" v-model="memory_min_num" placeholder="最小数" /></el-col>
        </el-row>

        <el-row class="py-1">
            <el-col :span="6">
              <el-tooltip   class="box-item"  effect="dark"  content="页面加载超时:等待超时时间默认是30s。单位是秒。用户可以代理IP质量进行调整。0表示使用默认值,最大值10000s"  placement="top-start" >
                <el-text tag="sub" size="small" class="underline white">页面加载超时</el-text>
              </el-tooltip>
            </el-col>
            <el-col :span="18"><el-input-number tag="sub" :min="0" :max="10000" class="w-50" size="small" v-model="page_load_timeout" placeholder="0取默认值" /></el-col>
        </el-row>

        <el-row class="py-1">
            <el-col :span="6">
              <el-tooltip class="box-item"  effect="dark"  content="代理IP池接口:1,空值表示不使用代理IP池,不使用代理IP池快排成功率低。2,使用代理IP,关于代理IP的通道用户自行选择"  placement="top-start" >
                <el-text tag="sub" size="small" class="underline white">代理IP池接口</el-text>
              </el-tooltip>
            </el-col>
            <el-col :span="18">
               <el-input :rows="2"  type="textarea"  class="pr-2  leading-10"  v-model="proxy_url" placeholder="代理池url地址,选填" /><br>
<!--               <el-link type="primary"   class="text-[0.725rem] text-red-700 " @click="dialog_ipTest_visible=true" >测试</el-link>-->
            </el-col>
        </el-row>

         <el-row class="py-1">
            <el-col :span="6">
              <el-tooltip   class="box-item"  effect="dark"  content="代理IP间隔:代理IP间隔时间。单位是秒。0表示不使用间隔功能。1,很多经济性代理要求在多少秒请问一次。可以设置请求频率最大减小用户的money开销"  placement="top-start" >
                <el-text tag="sub" size="small" class="underline white">代理IP间隔</el-text>
              </el-tooltip>
            </el-col>
            <el-col :span="18"><el-input-number tag="sub" :min="0" :max="5" class="w-50" size="small" v-model="proxy_ip_interval" placeholder="0取默认值" /></el-col>
        </el-row>

       <el-row class="py-1">
            <h5 class="text-blue-500 mt-2 text-[.8rem]">
              保存的配置，需要重启程序后才能生效。
            </h5>
         </el-row>
      
         <el-row class="py-4">
            <el-col :span="6"></el-col>
            <el-col :span="18">
                <el-button  size="small" round class="bg-blue-500 text-white ml-8 mr-4 " style="padding:1rem" @click="confSave" >保存</el-button>
                <el-button  size="small" round @click="resetConf" style="padding:1rem;color:white">重置</el-button>
            </el-col>
         </el-row>
      
    </div>

    <IpTest v-show="dialog_ipTest_visible" />
</template>

<script setup>
import { ref, onMounted,getCurrentInstance } from 'vue';
import { ElMessage } from 'element-plus'
import { useBdStore } from '@/stores/bdStore.js';
import IpTest from './IpTest.vue'
// import IpTestList from './ipTestList.vue'

const BDStore = useBdStore();
const search_stay_min_num = ref(0) //停留时间
const search_stay_max_num = ref(0)
const depth_max = ref(0)
const threads_max_num = ref(0)
const memory_min_num = ref(0)
const proxy_url = ref('')
const page_load_timeout = ref(30)
const proxy_ip_interval = ref(0)
const isUserCustom = ref(0)
const dialog_ipTest_visible = ref(false)


const BDStoreInit = (()=>{
    search_stay_min_num.value = BDStore.search_stay_min_num //停留时间
    search_stay_max_num.value = BDStore.search_stay_max_num
    depth_max.value = BDStore.depth_max
    threads_max_num.value = BDStore.threads_max_num
    memory_min_num.value = BDStore.memory_min_num
    proxy_url.value = ''
    page_load_timeout.value = BDStore.page_load_timeout
    proxy_ip_interval.value = BDStore.proxy_ip_interval
    BDStore.currentMenuPath = 'conf'

    // window.pywebview.api.pybdSetconfCache('w',BDStore).then((r) => {
    //         if (r) {
    //            // ElMessage({
    //            //    message: '第一次保存，数据配置保存成功',
    //            //    type: 'success',
    //            //  })
    //         }
    //     })
})

const confSave = (() => {
    BDStore.$patch((state)=>{
        state.search_stay_min_num = search_stay_min_num.value
        state.search_stay_max_num = search_stay_max_num.value
        state.depth_max = depth_max.value
        state.threads_max_num = threads_max_num.value
        state.memory_min_num = memory_min_num.value
        state.page_load_timeout = page_load_timeout.value
        state.proxy_ip_interval = proxy_ip_interval.value
        state.proxy_url = proxy_url.value
        state.isUserCustom = 1
        window.pywebview.api.pybdSetconfCache('w',state).then((r) => {
            if (r) {
               ElMessage({
                  message: '数据配置保存成功,需要在下次启动时生效',
                  type: 'success',
                })
            }
        })
    })

})

const resetConf = (()=>{
  BDStore.$reset()
  BDStore.currentMenuPath = 'conf'
  ElMessage({
      message: '数据配置重置完成',
      type: 'warning',
    })
 
  // ElMessage.error('Oops, this is a error message.')
   BDStoreInit()
})


onMounted(() => {
 
  setTimeout( () => {
      bdSetconfCache()
  }  ,300)


})

 

const bdSetconfCache = (() => {
    window.pywebview.api.pybdSetconfCache('r').then((r) => {
            if (r) {
                search_stay_min_num.value = r.search_stay_min_num -0
                search_stay_max_num.value = r.search_stay_max_num -0
                depth_max.value = r.depth_max -0
                threads_max_num.value = r.threads_max_num -0
                memory_min_num.value = r.memory_min_num -0
                page_load_timeout.value = r.page_load_timeout -0
                proxy_ip_interval.value = r.proxy_ip_interval -0
                proxy_url.value = r.proxy_url
                isUserCustom.value = 1
            } else {
                BDStoreInit()
            }
        })

    //
    // window.pywebview.api.pymaterial_getBdConfigs().then((r) => {
    //   if (r==null || r == false) {
    //       BDStoreInit()
    //   } else {
    //     search_stay_min_num.value = r.search_stay_min_num -0
    //     search_stay_max_num.value = r.search_stay_max_num -0
    //     depth_max.value = r.depth_max -0
    //     threads_max_num.value = r.threads_max_num -0
    //     memory_min_num.value = r.memory_min_num -0
    //     page_load_timeout.value = r.page_load_timeout -0
    //     proxy_ip_interval.value = r.proxy_ip_interval -0
    //     isUserCustom.value = 1
    //   }
    // })

})


</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 480px;
}

:deep(.el-dialog) {
   background-color: #333333;
}
:deep(.el-dialog__body) {
  padding:0px !important;
  margin: 0px !important;
}

:deep(.el-table__body) {
  user-select: text;
  font-size: 0.675rem;
}

.white {
  color: white;
}

</style>

